import json

from . import settings
from .hrunutils import json2File
from .parser.hrunParser_excel import HrunParser_excel
from .parser.parser import Format


class HrunTestCase:
    """
    config
    testcase

    from excel yaml json
    to excel json
    """

    def __init__(self, tc_content=None):
        self.ObjTestcase = tc_content
        self.workdir = settings.workDir

    def toJsonString(self):
        jstr = json.dumps(self.ObjTestcase, skipkeys=False, check_circular=True, allow_nan=True,
                          indent=4, separators=(',', ':'), ensure_ascii=False)
        return jstr

    def toJsonFile(self, outjsonfile):
        json2File(outjsonfile, self.ObjTestcase)

    def toYamlFile(self, outfile):
        pass

    def fromJson2Excel(self, sheet):
        pass

    def fromSheet2List(self, sheet):
        return HrunParser_excel(self.workdir).fromSheet2List_prepare(sheet)

    def fromSheet2sheetSuite(self, sheet, wb):
        return HrunParser_excel(self.workdir).fromSheet2Dict_sheetsuite(sheet, wb)

    def fromSheet2tSuite(self, sheet, wb):
        return HrunParser_excel(self.workdir).fromSheet2List_tsuite(sheet, wb)

    def fromSwaggerJson(self):
        pass


def addapi(apis, nodeid, projectid):
    from .hruntestcasefilter import fineFastTCBody
    # from fastrunner import models

    for xapi in apis['teststeps']:
        api = Format(fineFastTCBody(xapi))
        api.parse()

        api_body = {
            'name': api.name,
            'body': api.testcase,
            'url': api.url,
            'method': api.method,
            # 'project': models.Project.objects.get(id=projectid),
            'relation': nodeid
        }

        print(api_body)

        # models.API.objects.create(**api_body)

    return
